package com.xhwy.service.impl;

import com.xhwy.entity.Product;
import com.xhwy.entity.ProductImage;
import com.xhwy.mapper.ProductImageMapper;
import com.xhwy.param.ProductImageParam;
import com.xhwy.param.ProductParam;
import com.xhwy.entity.ProductType;
import com.xhwy.enums.ProductState;
import com.xhwy.mapper.ProductMapper;
import com.xhwy.mapper.ProductTypeMapper;
import com.xhwy.param.ProductIdParam;
import com.xhwy.param.ProductQueryParam;
import com.xhwy.service.ProductService;
import com.xhwy.util.PageUtil;
import com.xhwy.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Service    //业务层
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private ProductImageMapper productImageMapper;

    @Override
    public PageUtil<Product> queryAllByLimit(Product product, PageUtil<Product> pageUtil) {
        List<Product> productList = productMapper.queryAllByLimit(product, pageUtil);
        int count = productMapper.count(product);
        //获取商品集合中的所有商品类型id转换为set集合
        Set<Integer> productTypeIdSet = productList.stream().map(Product::getProductTypeId).collect(Collectors.toSet());
        //查询商品类型信息
        List<ProductType> productTypeList = productTypeMapper.getByProductTypeIdCollection(productTypeIdSet);
        Map<Integer, ProductType> productTypeMap = productTypeList.stream().collect(Collectors.toMap(ProductType::getProductTypeId, v -> v));
        //设置商品的分类信息
        for (Product pro : productList) {
            pro.setProductType(productTypeMap.get(pro.getProductTypeId()));
        }

        pageUtil.setCode("001");
        pageUtil.setList(productList);
        pageUtil.setTotalRecords(count);

        return pageUtil;
    }

    @Override
    public R deleteProductById(Integer productId) {
        if(productId == null) return R.fail("数据异常");
        int i = productMapper.deleteProductById(productId);
        if(i > 0) return R.ok("删除成功");
        return R.fail("删除失败");
    }

    /**
     * 添加商品
     * @param productParam
     * @return
     */
    @Override
    public R addProduct(ProductParam productParam) {
        Product product = new Product();
        //拷贝相同属性
        BeanUtils.copyProperties(productParam, product);
        //设置创建时间
        product.setCreateTime(new Timestamp(new Date().getTime()));
        //默认不开启推荐
        product.setIsRecommend(0);
        //设置正常状态
        product.setProductState(ProductState.NORMAL.getStateCode());
        //设置销量
        product.setSalesVolume(0);
        List<ProductImageParam> pics = productParam.getPics();
        //设置主图：上传的第一张图片
        product.setMainImage(pics.get(0).getPic());
        int rows = productMapper.addProduct(product);
        if(rows > 0) {
            //添加商品图片
            List<ProductImage> productImageList = new ArrayList<>();
            if(pics.size() > 1){
                Integer productId = product.getProductId();
                for (int i = 1; i < pics.size(); i++) {
                    //创建商品图片对象
                    ProductImage productImage = new ProductImage();
                    productImage.setProductId(productId);
                    productImage.setImagePath(pics.get(i).getPic());
                    productImage.setCreateTime(new Timestamp(new Date().getTime()));
                    productImage.setPriority(pics.size() - i);
                    //添加到集合中
                    productImageList.add(productImage);
                }
                //执行添加操作
                rows = productImageMapper.batchInsert(productImageList);
            }
            if(rows> 0) return R.ok("添加成功!");
        }
        return R.fail("添加失败");
    }

    /**
     * 获取推荐列表商品
     *
     * @return
     */
    @Override
    public R getRecommendList() {
        List<Product> list = productMapper.getRecommendList(5);
        return R.ok("查询成功", list);
    }

    /**
     * 获取热销商品列表
     *
     * @return
     */
    @Override
    public R getHotProductList() {
        List<Product> list = productMapper.getHotProductList(5);
        return R.ok("查询成功", list);
    }

    /**
     * 通用查询商品接口
     * 传入productTypeId，根据商品类型查询商品
     * 传入keyword关键字，根据关键字模糊查询商品
     *
     * @param productQueryParam
     * @return
     */
    @Override
    public R getByProductType(ProductQueryParam productQueryParam) {
        List<Product> list = productMapper.getByProductType(productQueryParam);
        int total = productMapper.getCountByProductType(productQueryParam);
        R r = R.ok("查询数据成功", list);
        r.put("total", total);
        return r;
    }

    /**
     * 根据商品id，查询商品详情信息
     *
     * @param productIdParam 已经校验过的商品id
     * @return
     */
    @Override
    public R detail(ProductIdParam productIdParam) {
        Product product = productMapper.getByProductId(productIdParam.getProductId());
        return R.ok("查询成功", product);
    }

    /**
     * 开启/关闭 推荐
     *
     * @param productIdParam 已经校验过的商品id
     * @return
     */
    @Override
    public R recommend(ProductIdParam productIdParam) {
        //获取商品信息
        Product product = productMapper.getByProductId(productIdParam.getProductId());
        if(product == null) return R.fail("操作失败，该商品不存在!");
        Product newProduct = new Product();
        newProduct.setProductId(product.getProductId());
        R r = null;
        if(product.getIsRecommend() == 1){
            r = R.ok("关闭推荐操作成功!");
            newProduct.setIsRecommend(0);
        }else {
            r = R.ok("开启推荐操作成功!");
            newProduct.setIsRecommend(1);
        }
        //执行修改操作
        int rows = productMapper.updateProduct(newProduct);
        if (rows <= 0) return R.fail("操作失败");
        return r;
    }

    /**
     * 上架/下架商品
     *
     * @param productIdParam 已经校验过的商品id
     * @return
     */
    @Override
    public R changeSellState(ProductIdParam productIdParam) {
        //获取商品信息
        Product product = productMapper.getByProductId(productIdParam.getProductId());
        if(product == null) return R.fail("操作失败，该商品不存在!");
        Product newProduct = new Product();
        newProduct.setProductId(product.getProductId());
        R r = null;
        if(product.getProductState() == ProductState.NORMAL.getStateCode()){
            r = R.ok("下架商品成功!");
            newProduct.setProductState(ProductState.SELLDOWN.getStateCode());
        }else {
            r = R.ok("上架商品成功!");
            newProduct.setProductState(ProductState.NORMAL.getStateCode());
        }
        //执行修改操作
        int rows = productMapper.updateProduct(newProduct);
        if (rows <= 0) return R.fail("操作失败");
        return r;
    }
}
